package org.Yiran.timetale_re.worldgen;

import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderGetter;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.data.worldgen.placement.PlacementUtils;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.valueproviders.ConstantInt;
import net.minecraft.world.level.levelgen.VerticalAnchor;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.placement.*;
import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import org.Yiran.timetale_re.Timetale_re;
import org.Yiran.timetale_re.item.material.ModMaterials;

import java.util.List;

/**
 * 放置特征注册类
 * <p>
 * 这个类用于注册和管理所有放置特征（PlacedFeature）。
 * 放置特征定义了配置特征在哪里以及如何生成（例如高度、密度等）。
 * </p>
 */
public class ModPlacedFeatures {
    
    /**
     * 放置特征注册器
     */
    public static final DeferredRegister<PlacedFeature> PLACED_FEATURES = 
            DeferredRegister.create(Registries.PLACED_FEATURE, Timetale_re.MODID);

    // 银矿石放置特征
    public static final ResourceKey<PlacedFeature> OVERWORLD_SILVER_ORE_PLACED_KEY = createKey("silver_ore");
    public static final ResourceKey<PlacedFeature> OVERWORLD_DEEPSLATE_SILVER_ORE_PLACED_KEY = createKey("deepslate_silver_ore");
    
    // 铝矿石放置特征
    public static final ResourceKey<PlacedFeature> OVERWORLD_ALUMINUM_ORE_PLACED_KEY = createKey("aluminum_ore");
    public static final ResourceKey<PlacedFeature> OVERWORLD_DEEPSLATE_ALUMINUM_ORE_PLACED_KEY = createKey("deepslate_aluminum_ore");
    
    // 钛矿石放置特征
    public static final ResourceKey<PlacedFeature> OVERWORLD_TITANIUM_ORE_PLACED_KEY = createKey("titanium_ore");
    public static final ResourceKey<PlacedFeature> OVERWORLD_DEEPSLATE_TITANIUM_ORE_PLACED_KEY = createKey("deepslate_titanium_ore");
    
    // 水晶簇放置特征
    public static final ResourceKey<PlacedFeature> CRYSTAL_CLUSTER_PLACED_KEY = createKey("crystal_cluster");
    
    // 能晶簇放置特征
    public static final ResourceKey<PlacedFeature> OVERWORLD_ENERGY_CRYSTAL_CLUSTER_PLACED_KEY = createKey("overworld_energy_crystal_cluster");
    public static final ResourceKey<PlacedFeature> NETHER_ENERGY_CRYSTAL_CLUSTER_PLACED_KEY = createKey("nether_energy_crystal_cluster");

    /**
     * 注册放置特征
     * <p>
     * 此方法应在模组主类的构造函数中调用，以正确注册所有放置特征
     * </p>
     *
     * @param modEventBus 模组事件总线
     */
    public static void register(IEventBus modEventBus) {
        PLACED_FEATURES.register(modEventBus);
    }
    
    /**
     * 创建放置特征注册键
     * <p>
     * 创建一个用于注册放置特征的资源键
     * </p>
     *
     * @param name 特征名称
     * @return 放置特征的资源键
     */
    public static ResourceKey<PlacedFeature> createKey(String name) {
        return ResourceKey.create(Registries.PLACED_FEATURE, ResourceLocation.fromNamespaceAndPath(Timetale_re.MODID, name));
    }
    
    /**
     * 引导放置特征
     * <p>
     * 用于数据生成的引导方法
     * </p>
     *
     * @param context 引导上下文
     */
    public static void bootstrap(BootstapContext<PlacedFeature> context) {
        HolderGetter<ConfiguredFeature<?, ?>> configuredFeatures = context.lookup(Registries.CONFIGURED_FEATURE);
        
        // 注册银矿石放置特征
        if (ModMaterials.SILVER_MATERIAL.getOre() != null) {
            Holder<ConfiguredFeature<?, ?>> silverOreHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.OVERWORLD_SILVER_ORE_KEY);
            PlacementUtils.register(context, OVERWORLD_SILVER_ORE_PLACED_KEY, silverOreHolder,
                    List.of(
                        CountPlacement.of(ModMaterials.SILVER_MATERIAL.getVeinCount()),
                        InSquarePlacement.spread(),
                        HeightRangePlacement.uniform(
                            VerticalAnchor.absolute(ModMaterials.SILVER_MATERIAL.getMinHeight()), 
                            VerticalAnchor.absolute(ModMaterials.SILVER_MATERIAL.getMaxHeight())),
                        BiomeFilter.biome()
                    ));
        }
        
        if (ModMaterials.SILVER_MATERIAL.getDeepslateOre() != null) {
            Holder<ConfiguredFeature<?, ?>> deepslateSilverOreHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.OVERWORLD_DEEPSLATE_SILVER_ORE_KEY);
            PlacementUtils.register(context, OVERWORLD_DEEPSLATE_SILVER_ORE_PLACED_KEY, deepslateSilverOreHolder,
                    List.of(
                        CountPlacement.of(ModMaterials.SILVER_MATERIAL.getVeinCount()),
                        InSquarePlacement.spread(),
                        HeightRangePlacement.uniform(
                            VerticalAnchor.absolute(ModMaterials.SILVER_MATERIAL.getMinHeight()), 
                            VerticalAnchor.absolute(ModMaterials.SILVER_MATERIAL.getMaxHeight())),
                        BiomeFilter.biome()
                    ));
        }
        
        // 注册铝矿石放置特征
        if (ModMaterials.ALUMINUM_MATERIAL.getOre() != null) {
            Holder<ConfiguredFeature<?, ?>> aluminumOreHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.OVERWORLD_ALUMINUM_ORE_KEY);
            PlacementUtils.register(context, OVERWORLD_ALUMINUM_ORE_PLACED_KEY, aluminumOreHolder,
                    List.of(
                        CountPlacement.of(ModMaterials.ALUMINUM_MATERIAL.getVeinCount()),
                        InSquarePlacement.spread(),
                        HeightRangePlacement.uniform(
                            VerticalAnchor.absolute(ModMaterials.ALUMINUM_MATERIAL.getMinHeight()), 
                            VerticalAnchor.absolute(ModMaterials.ALUMINUM_MATERIAL.getMaxHeight())),
                        BiomeFilter.biome()
                    ));
        }
        
        if (ModMaterials.ALUMINUM_MATERIAL.getDeepslateOre() != null) {
            Holder<ConfiguredFeature<?, ?>> deepslateAluminumOreHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.OVERWORLD_DEEPSLATE_ALUMINUM_ORE_KEY);
            PlacementUtils.register(context, OVERWORLD_DEEPSLATE_ALUMINUM_ORE_PLACED_KEY, deepslateAluminumOreHolder,
                    List.of(
                        CountPlacement.of(ModMaterials.ALUMINUM_MATERIAL.getVeinCount()),
                        InSquarePlacement.spread(),
                        HeightRangePlacement.uniform(
                            VerticalAnchor.absolute(ModMaterials.ALUMINUM_MATERIAL.getMinHeight()), 
                            VerticalAnchor.absolute(ModMaterials.ALUMINUM_MATERIAL.getMaxHeight())),
                        BiomeFilter.biome()
                    ));
        }
        
        // 注册钛矿石放置特征
        if (ModMaterials.TITANIUM_MATERIAL.getOre() != null) {
            Holder<ConfiguredFeature<?, ?>> titaniumOreHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.OVERWORLD_TITANIUM_ORE_KEY);
            PlacementUtils.register(context, OVERWORLD_TITANIUM_ORE_PLACED_KEY, titaniumOreHolder,
                    List.of(
                        CountPlacement.of(ModMaterials.TITANIUM_MATERIAL.getVeinCount()),
                        InSquarePlacement.spread(),
                        HeightRangePlacement.uniform(
                            VerticalAnchor.absolute(ModMaterials.TITANIUM_MATERIAL.getMinHeight()), 
                            VerticalAnchor.absolute(ModMaterials.TITANIUM_MATERIAL.getMaxHeight())),
                        BiomeFilter.biome()
                    ));
        }
        
        if (ModMaterials.TITANIUM_MATERIAL.getDeepslateOre() != null) {
            Holder<ConfiguredFeature<?, ?>> deepslateTitaniumOreHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.OVERWORLD_DEEPSLATE_TITANIUM_ORE_KEY);
            PlacementUtils.register(context, OVERWORLD_DEEPSLATE_TITANIUM_ORE_PLACED_KEY, deepslateTitaniumOreHolder,
                    List.of(
                        CountPlacement.of(ModMaterials.TITANIUM_MATERIAL.getVeinCount()),
                        InSquarePlacement.spread(),
                        HeightRangePlacement.uniform(
                            VerticalAnchor.absolute(ModMaterials.TITANIUM_MATERIAL.getMinHeight()), 
                            VerticalAnchor.absolute(ModMaterials.TITANIUM_MATERIAL.getMaxHeight())),
                        BiomeFilter.biome()
                    ));
        }
        
        // 注册水晶簇放置特征
        Holder<ConfiguredFeature<?, ?>> crystalClusterHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.CRYSTAL_CLUSTER_KEY);
        PlacementUtils.register(context, CRYSTAL_CLUSTER_PLACED_KEY, crystalClusterHolder,
                List.of(
                    CountPlacement.of(8), // 每区块尝试生成次数
                    InSquarePlacement.spread(),
                    HeightRangePlacement.uniform(
                        VerticalAnchor.aboveBottom(4), // 最低高度
                        VerticalAnchor.belowTop(4)),   // 最高高度，扩展到地表
                    BlockPredicateFilter.forPredicate(BlockPredicate.ONLY_IN_AIR_PREDICATE),
                    BiomeFilter.biome()
                ));
        
        // 注册能晶簇放置特征 - 主世界（提高生成概率）
        Holder<ConfiguredFeature<?, ?>> overworldEnergyCrystalClusterHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.OVERWORLD_ENERGY_CRYSTAL_CLUSTER_KEY);
        PlacementUtils.register(context, OVERWORLD_ENERGY_CRYSTAL_CLUSTER_PLACED_KEY, overworldEnergyCrystalClusterHolder,
                List.of(
                    CountPlacement.of(25), // 每区块尝试生成次数，提高频率
                    InSquarePlacement.spread(),
                    HeightRangePlacement.uniform(
                        VerticalAnchor.aboveBottom(0), // 最低高度
                        VerticalAnchor.belowTop(0)),   // 最高高度，无限制
                    // 移除BlockPredicateFilter，使生成更容易成功
                    BiomeFilter.biome()
                ));
                
        // 注册能晶簇放置特征 - 下界（提高生成概率）
        Holder<ConfiguredFeature<?, ?>> netherEnergyCrystalClusterHolder = configuredFeatures.getOrThrow(ModConfiguredFeatures.NETHER_ENERGY_CRYSTAL_CLUSTER_KEY);
        PlacementUtils.register(context, NETHER_ENERGY_CRYSTAL_CLUSTER_PLACED_KEY, netherEnergyCrystalClusterHolder,
                List.of(
                    CountPlacement.of(30), // 每区块尝试生成次数，提高频率以适应下界环境
                    InSquarePlacement.spread(),
                    HeightRangePlacement.uniform(
                        VerticalAnchor.aboveBottom(0), // 最低高度
                        VerticalAnchor.belowTop(0)),   // 最高高度，无限制
                    // 移除BlockPredicateFilter，使生成更容易成功
                    BiomeFilter.biome()
                ));
    }
}